%[main skip] = ringMarker(piece,main,pos)
%   This function finds the ringmarkers that have been detected by
%   'readPiece' and returns them in 'main'. The variable 'skip' contains
%   the value that is needed to cut away the ringmarkers in 'piece' after
%   they are stored.

%%

function [main skip] = ringMarker(piece,main,pos)

%% Initialisation
%This function needs the field 'ringmarker' and therefore makes sure it
%exists
if ~myIsField(main,'ringmarker')
    main(pos).ringmarker = [];
end

%'skip' Starts at 0
skip = 0;

%% Main

%If the first element of 'piece' signifies a ringmarker with integer >= 10,
%which in SMILES is then meaning of a percentage mark:
if piece(1) == '%'
    %Find the largest integer that makes sence from this percentage mark to
    %the end of 'piece' and 'break' when more than a ringmarker is
    %selected.
    for j = 2:length(piece)
        testmarker = str2double(piece(2:j));
        if isnan(testmarker)
            break
        else
            marker = testmarker;
        end
    end
    
    %If the marker makes sense it is stored.
    if ~isnan(marker)
        main(pos).ringmarker = [main(pos).ringmarker marker];
    end
    %The correct value of 'skip' is stored.
    skip = length(num2str(marker))+1;
else
    %If the marker is an integer < 10 find the ringmarker, store it and
    %appoint the correct value to skip.
    for j = 1:length(piece)
        marker = str2double(piece(j));
        if isnan(marker)
            break
        else
            main(pos).ringmarker = [main(pos).ringmarker marker];
            skip = skip+1;
        end
    end
end

%% End
end